package com.yf.customdubbo.registry;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;

import java.util.ArrayList;
import java.util.List;

public class ServiceDiscoveryImpl implements IServiceDiscovery {

    List<String> lists=new ArrayList<>();

    private CuratorFramework curatorFramework;

    public ServiceDiscoveryImpl(){
        curatorFramework= CuratorFrameworkFactory.builder().connectString(ZkConfig.zkAddress).
                sessionTimeoutMs(4000).retryPolicy(new ExponentialBackoffRetry(1000,10))
                .build();
        curatorFramework.start();
    }


    @Override
    public String discovery(String serviceName) {
        //根据serviceName拼接
        String path=ZkConfig.zkRegistryPath+"/"+serviceName;
        try{
            lists=curatorFramework.getChildren().forPath(path);

        }catch (Exception e){
            e.printStackTrace();
        }
        //TODO 负载均衡   子节点很多的情况下，需要进行loadBalance
        return lists.get(0);
    }
}
